<template>
	<view class="wrap">
		<block v-if="!isPayFlag">
			<view class="modItem">
				<view class="modTit">订单信息</view>
				<view class="moCon">
					<order-info currType="pay" :buyDays="buyDays" :orderNoLocal="orderNoLocal" :vipEndDate="vipEndDate"></order-info>
				</view>
			</view>
			<view class="modItem">
				<view class="modTit">支付方式</view>
				<view class="moCon">
					<sel-pay :payTypeList="payTypeList" :payTypeIndex="payTypeIndex" @selPayType="selPayType"></sel-pay>
				</view>
			</view>
			<bot-btn :actMoney="actMoney" @goPay="goPay" btnTit="确认支付"></bot-btn>
		</block>
		<block v-else>
			<view class="payRes wholeWh">
				<block v-if="callBackFlag">
					<image class="payImg" src="../../../static/images/succ.png"></image>
					<view class="payPrice">¥{{orderCallInfo.orderMoney}}</view>
					<view class="payTit">订单支付成功</view>
					<view class="paySucSmTit">订单编号：{{orderCallInfo.orderNoLocal}}</view>
					<block v-if="fromPage == 'zxList'">
						<!-- 前台倾诉师详情进入 -->
						<button class="comBtn" @tap="goBack(3)">返回</button>
					</block>
					<block v-else-if="fromPage == 'zxOrder'">
						<button class="comBtn" @tap="goBack(1)">返回订单列表</button>
					</block>
					<block v-else>
						<!-- 倾诉订单详情 -->
						<button class="comBtn" @tap="goBack(1)">返回</button>
					</block>
				</block>
				<block v-else-if="callBackFlag == false">
					<image class="payImg" src="../../../static/images/fail.png"></image>
					<view class="failTipsBig">支付失败</view>
					<view class="failTxt">{{smFailTxt}}</view>
					<button class="comBtn" @tap="goBack(1)">返回</button>
				</block>
			</view>
		</block>
	</view>
</template>

<script>
	import OrderInfo from './components/OrderInfo'
	import SelPay from './components/SelPay'
	import BotBtn from './components/BotBtn'
	//import { comPay } from '@/api/pay.js'
	import { getStudyBuyOrderInfo,getStudyBuyOrderResult } from '@/api/zhuxue'
	export default {
		components:{
			OrderInfo, 
			SelPay,
			BotBtn
		},
		data() {
			return {
				payTypeList:[
					{id:'wx',name:'微信支付',icon:'icon-weixinzhifu'},
					{id:'zfb',name:'支付宝支付',icon:'icon-zhifubao'},
				],
				payTypeIndex:0,
				payType:'wx',
				actMoney:0,
				isPayFlag:false,
				callBackFlag:null,//true->返回200 false->返回其它code
				fromPage:'',//zxList zxOrder zxOrderDet
				smFailTxt:'',
				orderNoLocal:'',
				vipEndDate:'',
				orderCallInfo:[],
				isClickFlag:true,
				orderId:''
			}
		},
		onLoad(options){
			this.orderId = options.orderId;
			this.fromPage = options.fromPage;
			this.buyDays = options.buyDays;
			this.vipEndDate = options.vipEndDate;
			this.getOrderDet();
		},
		methods: {
			getOrderDet(){
				var field = { id:this.orderId };
				getStudyBuyOrderInfo(field).then(res=>{
					this.actMoney = res.data.datas.money;
					this.orderNoLocal = res.data.datas.orderNoLocal;
				}).catch(err=>{
					console.log(err)
				})
			},
			selChild(index,id){
				this.childIndex = index;
			},
			selPayType(index,id){
				this.payTypeIndex = index;
				this.payType = id;
			},
			goPay(){
				this.util.showToast('后续开放');
				// if(this.isClickFlag){
				// 	this.isClickFlag = false;
				// 	var url = this.payType == 'wx' ? '/wxPay/genStudyBuyInfo' : '/aliPay/genStudyBuyOrderInfo';
				// 	var field = { orderId:this.orderId };
				// 	comPay(url,field).then(res=>{
				// 		console.log(res)
				// 		this.beginPay(res.data.datas);
				// 	}).catch(err=>{
				// 		this.isClickFlag = true;//可再次点击
				// 		this.isPayFlag = true;
				// 		this.callBackFlag = false;
				// 		this.smFailTxt = err.code.message;
				// 	});
				// }
			},
			beginPay(orderInfo){
				console.log(orderInfo)
				var provider = this.payType == 'zfb' ? 'alipay' : 'wxpay';
				uni.requestPayment({
				    provider: provider,
					orderInfo:orderInfo,
					success: (res)=>{
						//this.isRefreshFlag = true;//如果是从订单页面进来 onUnload卸载时 让上一页面的isRefreshFlag为true从而刷新
						this.getBuyOrderResult();
						console.log('==================zfbResSt==================')
						console.log(res);
						console.log('==================zfbResEnd==================')
					},
					fail: (err)=>{
						this.isClickFlag = true;
						console.log('fail:' + JSON.stringify(err));
						//this.util.showToast(err.errMsg,5000);
					}
				});
			},
			getBuyOrderResult(){
				var field = { orderId:this.orderId };
				getStudyBuyOrderResult(field).then(res=>{
					this.succCallBack(res.data.datas);
				}).catch(err=>{
					this.failCallBack(err.code.message);
				});
			},
			succCallBack(resSucc){
				this.isPayFlag = true;
				this.callBackFlag = true;
				this.orderCallInfo = resSucc;
			},
			failCallBack(msg){
				this.isPayFlag = true;
				this.callBackFlag = false;
				this.smFailTxt = msg;
				this.isClickFlag = true;//可再次点击
			},
			goBack(delta){
				uni.navigateBack({
					delta
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $pss-bg-primary;
	}
</style>
<style lang="scss" scoped>
	@import '@/static/scss/payRes.scss';
	.modItem{
		background: #fff;
		border-radius: 15rpx;
		padding: 0 20rpx;
		margin-bottom: 30rpx;
		.moCon{
			padding-bottom: 20rpx;
		}
		.modTit{
			color: $pss-text-color3;
			font-weight: bold;
			@include respTo(phone){
				height: 90rpx;
				line-height: 90rpx;
				font-size: $font-size32;
			}
			@include respTo(pad){
				height: 60rpx;
				line-height: 60rpx;
				font-size: $padSize20;
			}
		}
	}
	.wrap{
		@include respTo(phone){
			padding: 30rpx 30rpx 0;
		}
		@include respTo(pad){
			padding: 20rpx 20rpx 0;
		}
	}
</style>
